<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class AdProvider extends Model
{
	const TYPE_SELF = 0;
	const TYPE_OTHER = 1;
	protected $fillable = [
		'type',
		'name',
		'identifier'
	];
	public static function getSelfList()
	{
		return self::where('type', self::TYPE_SELF)->get();
	}
	public static function getOtherList()
	{
		return self::where('type', self::TYPE_OTHER)->get();
	}

	public function getCost($positionId)
	{
		// return $this->hasMany('\App\Model\AdCost')->orderBy('id', 'desc');
		return \App\Model\AdCost::where([
			'ad_provider_id'	=>	$this->id,
			'ad_position_id'	=>	$positionId,
		])->first();
	}

	public function getCpm($positionId)
	{
		$cost = $this->getCost($positionId);
		$ctr = $this->getCtr($positionId);
		return $cost * $ctr * 1000;
	}

	public function getCtr($positionId)
	{
		return 1;
	}
}